extends CanvasLayer

# 单例模式
var _instance = null
static func get_instance():
	return Engine.get_singleton("LoadingManager")

# 引用
@onready var loading_overlay = $LoadingOverlay
@onready var loading_spinner = %LoadingSpinner

# 计数器，用于处理嵌套请求
var _request_count = 0

# 动画相关
var _rotation = 0.0
var _animation_speed = 5.0

func _ready():
	# 初始化时隐藏loading遮罩
	hide_loading()
	
	# 设置loading spinner的纹理（如果有）
	# 如果项目中有loading spinner的纹理，可以在这里设置
	# loading_spinner.texture = preload("res://assets/ui/loading_spinner.png")

func _process(delta):
	# 旋转loading spinner
	if loading_overlay.visible:
		_rotation += delta * _animation_speed
		loading_spinner.radial_initial_angle = _rotation * 360.0

# 显示loading遮罩
func show_loading():
	_request_count += 1
	if _request_count == 1:
		loading_overlay.visible = true

# 隐藏loading遮罩
func hide_loading():
	_request_count -= 1
	if _request_count <= 0:
		_request_count = 0
		loading_overlay.visible = false
